// 增加窗口背景图
const originItemListInit = Window_QuestData.prototype.initialize;
Window_QuestData.prototype.initialize = function (x, y, width, height) {
    originItemListInit.call(this, x, y, width, height);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap =
        ImageManager.loadBitmap('img/system/UI/', 'MenuBg_QuestData');
    this.addChildAt(this._backgroundSprite, 0);
    this._backgroundSprite.x = -17 - 8 - 17;
    this._backgroundSprite.y = -1 - 18;
    this.opacity = 0;
};

Window_QuestData.prototype.drawQuestData = function() {
    Window_QuestData._questDataFmt = 
      JSON.parse(Yanfly.Param.QuestDataWindow['Quest Data Format'] || "");
    let questData = $dataQuests[this._questId];
    if (!questData) return;
    let isMainQuest = (questData.type === 
        JSON.parse(Yanfly.Param.QuestListWindow[('Type Order')])[0]
        .replace(/\\I\[(\d+)\]/gi, '').trim()
        .replace(/\\C\[(\d+)\]/gi, '').trim());                               // 判断是否是主线任务
    let fmt = isMainQuest ? 
        (this.isNoVisibleObjectives() ? "%5" :"%5\n\n任务目标：\n%6") : Window_QuestData._questDataFmt;
    let wordwrap = fmt.match(/<(?:WordWrap)>/i);
    let title = questData.name;
    title = title.replace(/\\I\[(\d+)\]/gi, '').trim();
    title = title.replace(/\\C\[(\d+)\]/gi, '').trim();
    let difficulty = questData.difficulty;
    let from = questData.from;
    let location = questData.location;
    let description = this.getQuestDescription();
    let objectives = this.getQuestObjectives(wordwrap);

    //console.log($gameSystem.getQuestObjectives(this._questId).length)
    //if (isMainQuest && objectives=="\i[212]");

    let rewards = this.getQuestRewards(wordwrap);
    let subtext = this.getQuestSubtext();
    let text = fmt.format(title, difficulty, from, location, description,
      objectives, rewards, subtext);
    let textState = { index: 0 };
    textState.originalText = text;
    textState.text = this.convertEscapeCharacters(text);
    this.resetFontSettings();
    this._allTextHeight = this.calcTextHeight(textState, true);
    this._allTextHeight *= (wordwrap) ? 10 : 1;
    this.createContents();
    this.drawQuestTextEx(text, 0, 0);
};

// 判断是否有可见目标
Window_QuestData.prototype.isNoVisibleObjectives = function() {
  const lineData = $dataQuests[this._questId].objectives;
  const length = $gameSystem.getQuestObjectives(this._questId).length;
  return (length === 1) && (lineData[1] === '""');
};


// 激活时变换背景
const originWindowQuestDataActivate = Window_QuestData.prototype.activate;
Window_QuestData.prototype.activate = function() {
  originWindowQuestDataActivate.call(this);
  if(this._backgroundSprite){
    this._backgroundSprite.bitmap =
      ImageManager.loadBitmap('img/system/UI/', 'MenuBg_QuestData2');
  }
};

const originWindowQuestDataDectivate = Window_QuestData.prototype.deactivate;
Window_QuestData.prototype.deactivate = function() {
  originWindowQuestDataDectivate.call(this);
  if(this._backgroundSprite){
    this._backgroundSprite.bitmap =
      ImageManager.loadBitmap('img/system/UI/', 'MenuBg_QuestData');
  }
};

// 修正原版延迟读取的变量未实装的错误
Window_QuestData.prototype.setQuestId = function(id) {
  if (this._questId !== id) {
    this._questId = id;
    this._countdown = this._delayLoad;
    this.refresh();
  }
};


